package com.aischool.eduorg.api;

import java.util.Date;
import java.util.List;

import com.aischool.common.model.AiCoursePlan;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiClassService;
import com.aischool.service.AiCoursePlanService;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/api/eduorg/plancourse")
public class PlanCourseController extends Controller {

	@Inject
	AiCoursePlanService aiCoursePlanService;
	@Inject
	AiClassService	aiClassService;
	
	public void getPlan() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		int org_id = this.getAttrForInt("login_org_id");
		
		int initno = getInt("initno", -1);
		
		int class_id = getParaToInt("class_id",0);
		Date start = this.getDate("start");
		Date end = this.getDate("end");
		
		if (initno == 0) {
			List<Record> rds = aiClassService.getAll(org_id, 1);
			if (rds != null && rds.size()>0) { 
				class_id = rds.get(0).getInt("class_id");
				jr.setDicKV("listclass", rds); 
				jr.setDicKV("cur_classid", class_id);
			}
		}
		
		if (class_id > 0) {
			Object obj = aiCoursePlanService.getCoursePlan(class_id, start, end);
			if (obj != null) { jr.setDicKV("liststplan", obj); }
		}

		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
		
	}
	
	public void save() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		int login_user_id = this.getAttrForInt("login_user_id");
		
		if (getInt("class_id",0)<=0) {
			jr.appendMsg("请选择班级！");
		}else if (StrKit.isBlank(get("course_date"))) {
			jr.appendMsg("请选择日期！");
		}else if (getInt("plan_sort", 0)<=0) {
			jr.appendMsg("请选择课节！");
		}else if (getInt("course_id", 0)<=0) {
			jr.appendMsg("请选择课程！");
		}else {
			Date start = this.getDate("course_date");
			Record rd= aiCoursePlanService.getOne(getInt("class_id",0), start, getInt("plan_sort",0));
			AiCoursePlan info = getModel(AiCoursePlan.class, "", true);
			
			boolean bf = false;
			info.setClassesId(getInt("classes_id",0));
			info.setUpdateUserid(login_user_id);
			info.setUpdateTime(DateUtil.getTimestamp());
			info.removeNullValueAttrs();
			
			if (rd != null) {
				info.setAutoId(rd.getInt("auto_id"));
				bf = info.update();
			}else {
				bf = info.save();
			}
			
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				jr.setEnum(JsonCodeEnum.Faild);
			}
		}
		
		renderJson(jr.Dic);
	}
	
	public void del() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		if (getInt("class_id",0)<=0) {
			jr.appendMsg("请选择班级！");
		}else if (StrKit.isBlank(get("course_date"))) {
			jr.appendMsg("请选择日期！");
		}else if (getInt("plan_sort", 0)<=0) {
			jr.appendMsg("请选择课节！");
		}else {
			int ir = aiCoursePlanService.del(getInt("class_id",0), getDate("course_date"), getInt("plan_sort", 0));
			if (ir>0) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				jr.setEnum(JsonCodeEnum.Faild);
			}
		}
		
		renderJson(jr.Dic);
	}
}
